package com.skywolf.chem.usercenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.skywolf.chem.common.context.LoginUser;
import com.skywolf.chem.common.context.RequestContext;
import com.skywolf.chem.common.enums.ResponseCodeEnum;
import com.skywolf.chem.common.sysconfig.exception.BizException;
import com.skywolf.chem.common.sysconfig.utils.PasswordGenerator;
import com.skywolf.chem.usercenter.entity.User;
import com.skywolf.chem.usercenter.mapper.UserMapper;
import com.skywolf.chem.usercenter.pojo.param.PasswordChangeParam;
import com.skywolf.chem.usercenter.pojo.param.UserCreateParam;
import com.skywolf.chem.usercenter.pojo.param.UserQueryParam;
import com.skywolf.chem.usercenter.pojo.param.UserUpdateParam;
import com.skywolf.chem.usercenter.service.IUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 系统用户 服务实现类
 * </p>
 *
 * @author leo
 * @since 2019-11-24
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Override
    public void resetPassword(Long id) {
        checkExistUser(id);
        //生成加密的密码,默认密码123456
        PasswordGenerator.Passwords passwords = PasswordGenerator.genDefaultPassword();
        User user = this.getById(id);
        user.setSalt(passwords.getSalt());
        user.setPassword(passwords.getPassword());
        //更新用户密码
        this.updateById(user);
    }

    @Override
    public User getByUsername(String username) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username);
        return this.getOne(queryWrapper);
    }

    @Override
    public void changePassword(PasswordChangeParam passwordChangeParam) {
        LoginUser loginUser = RequestContext.getLoginUser();
        //验证旧密码是否正确
        if (!PasswordGenerator.vatify(passwordChangeParam.getOldPassword(), loginUser.getPassword(), loginUser.getSalt())) {
            throw new BizException(ResponseCodeEnum.USER_INVALID_OLD_PASSWORD);
        }
        //生成新的加密的密码
        PasswordGenerator.Passwords passwords = PasswordGenerator.genPassword(passwordChangeParam.getNewPassword());
        //构建user对象
        User user = new User();
        user.setId(loginUser.getId());
        user.setPassword(passwords.getPassword());
        user.setSalt(passwords.getSalt());
        this.updateById(user);
    }

    @Override
    public void checkExistUser(Long id) {
        if (this.getById(id) == null) {
            throw new BizException(ResponseCodeEnum.USER_NOT_FOUND);
        }
    }

    @Override
    public IPage<User> pageList(UserQueryParam userQueryParam) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        if (StringUtils.hasText(userQueryParam.getUsername())) {
            queryWrapper.like("username", userQueryParam.getUsername());
        }
        if (StringUtils.hasText(userQueryParam.getName())) {
            queryWrapper.like("name", userQueryParam.getName());
        }
        if (StringUtils.hasText(userQueryParam.getPhone())) {
            queryWrapper.like("phone", userQueryParam.getPhone());
        }
        if (null != userQueryParam.getStatus()) {
            queryWrapper.eq("status", userQueryParam.getStatus());
        }
        IPage<User> page = this.page(userQueryParam.page(), queryWrapper);
        return page;
    }

    @Override
    public Long createUser(UserCreateParam userCreateParam) {
        //判断用户是否存在，存在则抛出存在异常
        if (isExistUser(userCreateParam.getUsername())) {
            throw new BizException(ResponseCodeEnum.USER_DUPLICATE);
        }
        //生成加密的密码,默认密码123456
        PasswordGenerator.Passwords passwords = PasswordGenerator.genDefaultPassword();
        //构建user对象
        User user = new User();
        BeanUtils.copyProperties(userCreateParam, user);
        user.setPassword(passwords.getPassword());
        user.setSalt(passwords.getSalt());
        //保存user
        this.save(user);
        return user.getId();
    }


    @Override
    public void updateUser(UserUpdateParam userUpdateParam) {
        checkExistUser(userUpdateParam.getId());
        User user = new User();
        BeanUtils.copyProperties(userUpdateParam, user);
        this.updateById(user);
    }


    @Override
    public void deleteUser(Long id) {
        checkExistUser(id);
        this.removeById(id);
    }

    @Override
    public void forbidUser(Long id, Integer status) {
        checkExistUser(id);
        UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("status", status).eq("id", id);
        this.update(updateWrapper);
    }
    private boolean isExistUser(String username) {
        return this.getByUsername(username) != null;
    }


}
